package com.plugin.mybatis.generator;

import org.mybatis.generator.eclipse.core.merge.JavaFileMerger;
import org.mybatis.generator.exception.ShellException;
import org.mybatis.generator.internal.DefaultShellCallback;

import java.io.*;

/**
 * @author xiangshiming
 */
public class TestDefaultShellCallback135 extends DefaultShellCallback {
    private boolean modelCover = false;
    public TestDefaultShellCallback135(boolean overwrite,boolean flagModelCover) {
        super(overwrite);
        modelCover = flagModelCover;
    }

    public TestDefaultShellCallback135(boolean overwrite) {
        super(overwrite);
    }


    public boolean isMergeSupported() {
        return true;
    }


    /*
    1.3.5用法
    public String mergeJavaFile(String newFileSource, String existingFileFullPath, String[] javadocTags, String fileEncoding) throws ShellException {
        if(modelCover && newFileSource.contains("public class")){
            return newFileSource;
        }
        JavaFileMerger merger = new JavaFileMerger(newFileSource, existingFileFullPath, javadocTags, fileEncoding);
        String mergerFileString = merger.getMergedSource()
                .replaceAll("private", "\n\tprivate")
                .replaceAll("@ApiModelProperty", "\n\t@ApiModelProperty")
                .replaceAll("@Id", "\n\t@Id")
                .replaceAll("@Column", "\n\t@Column")
                .replaceAll("@LogicDelete", "\n\t@LogicDelete")
                .replaceAll("@Version", "\n\t@Version")
                .replaceAll("(\r?\n(\\s*\r?\n)+)", "\r\n\r\n");
        return mergerFileString;
    }*/

    public String mergeJavaFile(String newFileSource, File existingFile, String[] javadocTags, String fileEncoding) throws ShellException {
        if(modelCover && newFileSource.contains("public class")){
            return newFileSource;
        }

        if (existingFile!=null){
            JavaFileMerger fileMerger = new JavaFileMerger(newFileSource, existingFile.getAbsolutePath(), javadocTags, fileEncoding);
            return fileMerger.getMergedSource()
                    .replaceAll("private", "\n\tprivate")
                    .replaceAll("@ApiModelProperty", "\n\t@ApiModelProperty")
                    .replaceAll("@Id", "\n\t@Id")
                    .replaceAll("@Column", "\n\t@Column")
                    .replaceAll("@LogicDelete", "\n\t@LogicDelete")
                    .replaceAll("@Version", "\n\t@Version")
                    .replaceAll("@JsonFormat", "\n\t@JsonFormat")
                    .replaceAll("@NotNull", "\n\t@NotNull")
                    .replaceAll("@NotBlank", "\n\t@NotBlank")
                    .replaceAll("@KeySql", "\n\t@KeySql")
                    .replaceAll("@GeneratedValue", "\n\t@GeneratedValue")
                    .replaceAll("(\r?\n(\\s*\r?\n)+)", "\r\n\r\n");
        }else {
            return newFileSource;
        }
    }

    private String getExistingFileContents(File existingFile, String fileEncoding) throws ShellException {
        StringBuilder sb = new StringBuilder();
        try {
            FileInputStream fileInputStream = new FileInputStream(existingFile);
            InputStreamReader reader;
            if (fileEncoding == null) {
                reader = new InputStreamReader(fileInputStream);
            } else {
                reader = new InputStreamReader(fileInputStream, fileEncoding);
            }
            BufferedReader br = new BufferedReader(reader);
            char[] buffer = new char[1024];
            for (int returnedBytes = br.read(buffer); returnedBytes != -1; returnedBytes = br.read(buffer)) {
                sb.append(buffer, 0, returnedBytes);
            }
            br.close();
        } catch (IOException e) {
            throw new ShellException(String.format("read file %s error!", existingFile.getAbsoluteFile()));
        }
        return sb.toString();
    }
}
